import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:provide/provide.dart';
import 'package:study_flutter/yzhg/project/shop/pages/goodsdetail/DetailCommentWidget.dart';
import 'package:study_flutter/yzhg/project/shop/provide/GoodsDetailProvide.dart';

import 'DtailCommentBar.dart';
import 'GoodsDetailBottom.dart';
import 'GoodsExplain.dart';

class GoodsDetailWidget extends StatefulWidget {
  @override
  _GoodsDetailWidgetState createState() => _GoodsDetailWidgetState();
}

class _GoodsDetailWidgetState extends State<GoodsDetailWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Provide<GoodsDetailProvide>(builder: (context, child, childData) {
      return Scaffold(
        appBar: AppBar(
          leading: IconButton(
              icon: Icon(Icons.arrow_back),
              onPressed: () {
                //返回上一级页面
                Navigator.pop(context);
              }),
          title: Text(
            (childData.goodInfo == null)
                ? "商品详情"
                : childData.goodInfo.goodsName,
            textAlign: TextAlign.center,
            style: TextStyle(
                fontSize: ScreenUtil().setSp(28.0), color: Colors.white),
          ),
        ),
        body: (childData.goodInfo != null)
            ? Stack(
                children: [
                  Container(
                    child: ListView(
                      shrinkWrap: true,
                      children: [
                        _goodsImage(childData.goodInfo.image1),
                        _goodsName(childData.goodInfo.goodsName),
                        _goodsNum(childData.goodInfo.goodsSerialNumber),
                        _goodsPrice(childData.goodInfo.presentPrice,
                            childData.goodInfo.oriPrice),
                        GoodsExplain(),
                        DetailCommentBar(),
                        DetailCommentWidget()
                      ],
                    ),
                  ),
                  Positioned(
                    child: GoodsDetailBottom(),
                    left: 0,
                    bottom: 0,
                  )
                ],
              )
            : Center(
                child: Text("没有数据"),
              ),
      );
    });
  }

  /*商品图片*/
  Widget _goodsImage(url) {
    return Image.network(
      url,
      width: ScreenUtil().setWidth(740),
    );
  }

  /*商品名称*/
  Widget _goodsName(name) {
    return Container(
      width: ScreenUtil().setWidth(740),
      padding: EdgeInsets.only(left: 16.0),
      decoration: BoxDecoration(color: Colors.white),
      child: Text(
        name,
        style: TextStyle(
            fontSize: ScreenUtil().setSp(30.0),
            color: Colors.black,
            fontWeight: FontWeight.bold),
      ),
    );
  }

  /*商品编号*/
  Widget _goodsNum(num) {
    return Container(
      decoration: BoxDecoration(color: Colors.white),
      width: ScreenUtil().setWidth(730),
      padding: EdgeInsets.only(left: 16.0, top: 8.0),
      child: Text(
        "编号：$num",
        style: TextStyle(
            color: Colors.black38, fontSize: ScreenUtil().setSp(24.0)),
      ),
    );
  }

  /*价格  和   市场价*/
  Widget _goodsPrice(presentPrice, oriPrice) {
    return Container(
      decoration: BoxDecoration(color: Colors.white),
      padding: EdgeInsets.only(top: 12.0, left: 16.0),
      child: Row(
        children: <Widget>[
          Text(
            "¥ $presentPrice",
            style: TextStyle(
                fontSize: ScreenUtil().setSp(32.0), color: Colors.red),
          ),
          Padding(
            padding: EdgeInsets.only(left: 24.0),
            child: Text(
              "市场价:",
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(30.0), color: Colors.black),
            ),
          ),
          Padding(
            padding: EdgeInsets.only(left: 24.0),
            child: Text(
              "$oriPrice",
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(30.0),
                  color: Colors.grey,
                  decoration: TextDecoration.lineThrough),
            ),
          )
        ],
      ),
    );
  }
}
